# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Planet;
use strict;
use Wx;
use base 'Wx::Panel';
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL);
use Wx qw(wxALIGN_LEFT wxALIGN_CENTRE wxALIGN_RIGHT wxST_NO_AUTORESIZE);
use Tools;

sub __newNumPanel
{
	my $self = shift;
	my $panel = Wx::Panel->new($self, -1, [-1, -1], [-1, -1]);
	$panel->{Label_x} = Wx::StaticText->new($panel, -1, "XXXX", [-1, -1], [-1, -1], wxALIGN_RIGHT | wxST_NO_AUTORESIZE);
	$panel->{Label_x}->SetForegroundColour(Wx::Colour->new(192, 192, 192));
	$panel->{Label_colon} = Wx::StaticText->new($panel, -1, ":", [-1, -1], [-1, -1], wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
	$panel->{Label_colon}->SetForegroundColour(Wx::Colour->new(192, 192, 192));
	$panel->{Label_y} = Wx::StaticText->new($panel, -1, "YYYY", [-1, -1], [-1, -1], wxALIGN_LEFT | wxST_NO_AUTORESIZE);
	$panel->{Label_y}->SetForegroundColour(Wx::Colour->new(192, 192, 192));
	$panel->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$panel->{'Label_'.$_} = Wx::StaticText->new($panel, -1, '??') foreach qw(o e m t s);
	$panel->{Label_popgrowth} = Wx::StaticText->new($panel, -1, '??.??');
	$panel->{Label_popgrowth}->SetForegroundColour(Wx::Colour->new(255, 255, 255));
	my %colors = (
		'o' => Wx::Colour->new(0, 255, 0),
		'e' => Wx::Colour->new(0, 255, 255),
		'm' => Wx::Colour->new(255, 0, 0),
		't' => Wx::Colour->new(255, 255, 0),
		's' => Wx::Colour->new(255, 0, 255)
	);
	$panel->{'Label_'.$_}->SetForegroundColour($colors{$_}) foreach qw(o e m t s);
	$panel->{Sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
	$panel->{Sizer}->Add($panel->{Label_x}, 1, wxEXPAND | wxALL, 1);
	$panel->{Sizer}->Add($panel->{Label_colon}, 0, wxEXPAND | wxALL, 1);
	$panel->{Sizer}->Add($panel->{Label_y}, 1, wxEXPAND | wxALL, 1);
	$panel->{Sizer}->Add($panel->{'Label_'.$_}, 1, wxEXPAND | wxALL, 1) foreach qw(o e m t s);
	$panel->{Sizer}->Add($panel->{Label_popgrowth}, 1, wxEXPAND | wxALL, 1);
	$panel->SetSizer($panel->{Sizer});
	return $panel;
}

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $id = shift;
	my $self = $class->SUPER::new($parent, $id, [-1, -1], [200, -1]); 
	$self->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{LabelName} = Wx::StaticText->new($self, -1, "Name", [-1, -1], [200, -1], wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
	$self->{LabelName}->SetForegroundColour(Wx::Colour->new(192, 192, 192));
	$self->{GeoPanel} = __newNumPanel($self);
	$self->{LabelOwner} = Wx::StaticText->new($self, -1, "Owner", [-1, -1], [200, -1], wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
	$self->{LabelOwner}->SetForegroundColour(Wx::Colour->new(192, 192, 192));
	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{Sizer}->Add($self->{LabelName}, 0, wxEXPAND | wxALL, 1);
	$self->{Sizer}->Add($self->{GeoPanel}, 0, wxEXPAND | wxALL, 1);
	$self->{Sizer}->Add($self->{LabelOwner}, 0, wxEXPAND | wxALL, 1);
	$self->SetSizer($self->{Sizer});
	return $self;
}

sub setPlanet
{
	my $self = shift;
	my $planet = shift;
	# TODO: Clear previous info.
	return unless $planet;
	my $empire = $planet->getEmpire();
	$self->{GeoPanel}->{'Label_'.$_}->SetLabel(Tools::formatCoordinate($planet->getProp($_))) foreach qw(x y);
	$self->{LabelName}->SetLabel(Tools::formatPlanetName(map {$planet->getProp($_)} qw(x y name)));
	$self->{LabelOwner}->SetLabel(defined($planet->getProp('owner_name')) ? $planet->getProp('owner_name') : '?');
	my $id = $planet->getProp('owner_id'); 
	my $rel = defined($id) ? $empire->dipRelation($id) : -1;
	$rel = -1 unless defined $rel;
	my %colors = (
		-1 => Wx::Colour->new(192, 192, 192),
		0 => Wx::Colour->new(255, 0, 0),
		1 => Wx::Colour->new(0, 0, 255),
		2 => Wx::Colour->new(0, 255, 0),
		3 => Wx::Colour->new(0, 255, 255),
		4 => Wx::Colour->new(255, 255, 0)
	);
	$self->{LabelOwner}->SetForegroundColour($colors{$rel});
	$self->{GeoPanel}->{'Label_'.$_}->SetLabel(defined($planet->getProp($_)) ? sprintf "%02d", $planet->getProp($_) : '??') foreach qw(o e m t s);
	if (defined($planet->getProp('s')))
	{
		my ($topt, $dt, $dp)  = map {$empire->getProp($_)} qw(t_optimal t_delta bonus_multiply);
		my $t = $planet->getProp('t');
		my $food = $planet->getProp($empire->getProp('race_nature'));
		my $growth = (2 - abs($topt - $t)/$dt < 1 ? 2 - abs($topt - $t)/$dt : 1) * $food * 0.05 * (1 + $dp / 100) / (abs($topt - $t) > 2 * $dt ? 2 : 1);
		$self->{GeoPanel}->{Label_popgrowth}->SetLabel(sprintf "%2.2f", $growth);
	}
	else
	{
		if (defined($planet->getProp('img_surface')))
		{
			$self->{GeoPanel}->{'Label_s'}->SetLabel($planet->getProp('img_surface').'?');
		}
		$self->{GeoPanel}->{Label_popgrowth}->SetLabel("??.??");
	}
}

1;
